package com.lwz.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwz.eduservice.client.OrderClient;
import com.lwz.eduservice.entity.EduCourse;
import com.lwz.eduservice.entity.chapter.ChapterVo;
import com.lwz.eduservice.entity.frontvo.CourseFrontVo;
import com.lwz.eduservice.entity.frontvo.CourseWebVo;
import com.lwz.eduservice.service.EduChapterService;
import com.lwz.eduservice.service.EduCourseService;
import com.lwz.servicebase.exceptionhandler.MyException;
import com.lwz.util.JwtUtil;
import com.lwz.util.R;
import com.lwz.util.orderVo.CourseWebVoOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author Lw中
 * @date 2020/11/19 15:55
 */

@RestController
@CrossOrigin
@RequestMapping("/eduservice/courseFront")
public class CourseFrontController {

    @Autowired
    EduCourseService eduCourseService;

    @Autowired
    EduChapterService eduChapterService;

    @Autowired
    OrderClient orderClient;

    /**
     * 分页条件查询课程相关数据
     * @param page
     * @param limit
     * @param courseFrontVo
     * @return
     */
    @PostMapping("/getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<EduCourse> coursePage  = new Page<>(page, limit);
        Map<String, Object> map = eduCourseService.getFrontCourseInfo(coursePage, courseFrontVo);
        return R.ok().data(map);
    }

    /**
     * LWZ TODO : 2020/11/21 通过课程的id获取课程的详情以及课程所包含的章节和小节
     */

    /**
     * 通过课程的id获取课程的详情以及课程所包含的章节和小节
     * @param courseId
     * @return
     */
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        // 通过课程的id获取课程需要传递到前端的数据
        CourseWebVo courseWebVo = eduCourseService.getBaseCourseInfo(courseId);
        // 通过课程的id获取课程的章节和小节
        List<ChapterVo> chapterVoList = eduChapterService.getChapterVideoByCourseId(courseId);
        // 通过课程id和用户id查询课程是否已经被用户支付
        String id = JwtUtil.getMemberIdByJwtToken(request).getId();
        if (StringUtils.isEmpty(id)) {
            throw new MyException(20001, "请先登录");
        }
        boolean buyCourse = orderClient.isBuyCourse(courseId, id);
        return R.ok().data("courseWebVo", courseWebVo).data("chapterVoList", chapterVoList).data("buyCourse", buyCourse);
    }

    /**
     * 根据课程id获取课程返回前端的核心信息
     * @param courseId
     * @return
     */
    @PostMapping("/getOrderCourseInfo/{courseId}")
    public CourseWebVoOrder getOrderCourseInfo(@PathVariable String courseId) {
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        CourseWebVo baseCourseInfo = eduCourseService.getBaseCourseInfo(courseId);
        BeanUtils.copyProperties(baseCourseInfo, courseWebVoOrder);
        return courseWebVoOrder;
    }

}
